
var Misbug = new Object();

Misbug.DOM = new Object();

Misbug.DOM.GetChildNodesByClassName = function(parentNode, className, tagName, recursive)
{
	if (typeof(parentNode) != "undefined")
	{
		tagName = tagName.toLowerCase();

		var childNodesByTagName = new Array();
		
		if (recursive)
		{
			var childNodes = parentNode.getElementsByTagName(tagName);
			for (var i = 0; i < childNodes.length; i++)
			{
				if ($(childNodes[i]).hasClass(className))
				{
					childNodesByTagName.push($(childNodes[i]));
				}
			}
		}
		else
		{
			var childNodes = parentNode.childNodes;
			for (var i = 0; i < childNodes.length; i++)
			{
				if (childNodes[i].tagName && childNodes[i].tagName.toLowerCase() === tagName && $(childNodes[i]).hasClass(className))
				{
					childNodesByTagName.push($(childNodes[i]));
				}
			}
		}
		
		return(childNodesByTagName);
	}
	else
	{
		return(false);
	}
}

Misbug.DOM.GetFirstChildWithClassName = function(parentNode, className, tagName)
{
	if (typeof(parentNode) != "undefined" && parentNode.getElementsByTagName)
	{
		var elementsByTagName = parentNode.getElementsByTagName(tagName);
		for (var i = 0; i < elementsByTagName.length; i++)
		{
			if (elementsByTagName[i].hasClass(className))
			{
				return(elementsByTagName[i]);
			}
		}
	}
	else
	{
		return(false);
	}
}

Misbug.DOM.GetFirstParentWithClassName = function(childNode, className)
{
	var element = childNode;
	while (typeof(element) != "undefined")
	{
		if (element.hasClass(className))
		{
			return(element);
		}
		else
		{
			element = $(element.parentNode);
		}
	}
	return(false);
}

Misbug.Event = new Object();

Misbug.Event.MouseHoldHandler = function()
{
}

Misbug.UI = new Object();

Misbug.UI.colors = {
	'link': '#888',
	'hlink': '#fff',
	'alink': '#a4ff4d'
}

Misbug.UI.TabPanels = new Array();

Misbug.UI.SetActiveTab = function(TabPanelID, newActiveTab, hide)
{
	TabPanel = Misbug.UI.TabPanels[TabPanelID];
	
	if (typeof(newActiveTab) == 'number' && 0 <= newActiveTab && newActiveTab < TabPanel.tabElements.length)
	{
		if (TabPanel.activeTab !== false)
		{
			TabPanel.tabs[TabPanel.activeTab].tab.removeClass('active');
			TabPanel.tabs[TabPanel.activeTab].button.removeClass('active');
			TabPanel.tabs[TabPanel.activeTab].button.fx.start({
				'color': Misbug.UI.colors['link']
			});
		}
		
		if (newActiveTab != TabPanel.activeTab || TabPanel.activeTab === false)
		{
			Misbug.UI.ShowTabPanel(TabPanelID);
			
			TabPanel.tabs[newActiveTab].tab.addClass('active');
			TabPanel.tabs[newActiveTab].button.addClass('active');
			TabPanel.tabs[newActiveTab].button.fx.start({
				'color': Misbug.UI.colors['alink']
			});
			
			TabPanel.activeTab = newActiveTab;
		}
		else
		{
			Misbug.UI.HideTabPanel(TabPanelID);
			TabPanel.activeTab = false;
		}
	}
}

Misbug.UI.ShowTabPanel = function(TabPanelID)
{
	TabPanel = Misbug.UI.TabPanels[TabPanelID];
	
	if (TabPanel.visible != true)
	{
		TabPanel.tabsBackgroundElement.setStyle('opacity', '0');
		TabPanel.tabsBackgroundElement.setStyle('background-color', '#000');
		
		var fx = new Fx.Morph(TabPanel.tabsBackgroundElement, {duration: 500, wait: false});
		fx.start({
			'opacity': .6
		});
		
		TabPanel.visible = true;
	}
}

Misbug.UI.HideTabPanel = function(TabPanelID)
{
	TabPanel = Misbug.UI.TabPanels[TabPanelID];
	
	if (TabPanel.visible == true)
	{
		var fx = new Fx.Morph(TabPanel.tabsBackgroundElement, {duration: 500, wait: false});
		fx.start({
			'opacity': 0
		});
			
		TabPanel.visible = false;
	}
}

Misbug.UI.TabPanel = function(arguments)
{
	var TabPanel = this;
	
	if (arguments && arguments.id)
	{
		TabPanel.id = arguments.id;
		
		Misbug.UI.TabPanels[TabPanel.id] = TabPanel;
		
		TabPanel.tabPanelElement = document.getElementById(TabPanel.id);
		
		if (TabPanel.tabPanelElement)
		{
			TabPanel.tabs = new Array();
			TabPanel.activeTab = false;
			TabPanel.buttonsElement = Misbug.DOM.GetFirstChildWithClassName(TabPanel.tabPanelElement, 'buttons', 'div');
			TabPanel.tabsElement = Misbug.DOM.GetFirstChildWithClassName(TabPanel.tabPanelElement, 'tabs', 'div');
			TabPanel.tabsBackgroundElement = $(Misbug.DOM.GetFirstChildWithClassName(TabPanel.tabPanelElement, 'tabs-background', 'div'));
			
			if (TabPanel.buttonsElement && TabPanel.tabsElement)
			{
				TabPanel.tabElements = Misbug.DOM.GetChildNodesByClassName(TabPanel.tabsElement, 'tab', 'div');
				
				for (var i = 0; i < TabPanel.tabElements.length; i++)
				{
					var button = document.createElement('a');
					button.innerHTML = TabPanel.tabElements[i].title;
					button.href = '#';
					button.onclick = function(){return(false);};
					button.className = 'button';
					
					TabPanel.tabs[i] = {
						title: TabPanel.tabElements[i].title,
						tab: $(TabPanel.tabElements[i]),
						button: $(TabPanel.buttonsElement.appendChild(button))
					};
					
					TabPanel.tabElements[i].title = "";
					
					TabPanel.tabs[i].button.name = i;
					
					
					TabPanel.tabs[i].button.addEvent('mousedown', function()
					{
						if (Misbug.Event.MouseHoldHandler.clickPermission)
						{
							window.clearTimeout(Misbug.Event.MouseHoldHandler.timerID);
							Misbug.UI.SetActiveTab(Misbug.DOM.GetFirstParentWithClassName(Misbug.Event.MouseHoldHandler.target, 'tab-panel').id, parseInt(Misbug.Event.MouseHoldHandler.target.name, 10));
						}
					});
					
					TabPanel.tabs[i].button.fx = new Fx.Morph(TabPanel.tabs[i].button, {duration: 200, wait: false});
					
					TabPanel.tabs[i].button.addEvent('mouseenter', function()
					{
						Misbug.Event.MouseHoldHandler.target = $(this);
						
						if (!this.hasClass('active'))
						{
							this.fx.start({
								'color': Misbug.UI.colors['hlink']
							});
						}
						
						window.clearTimeout(Misbug.Event.MouseHoldHandler.timerID);
						Misbug.Event.MouseHoldHandler.timerID = window.setTimeout(function()
						{
							if (!Misbug.Event.MouseHoldHandler.target.hasClass('active'))
							{
								Misbug.UI.SetActiveTab(Misbug.DOM.GetFirstParentWithClassName(Misbug.Event.MouseHoldHandler.target, 'tab-panel').id, parseInt(Misbug.Event.MouseHoldHandler.target.name, 10));
							}
						}, 200);
						
						if (!this.hasClass('active'))
						{
							Misbug.Event.MouseHoldHandler.clickPermission = false;
							
							window.clearTimeout(Misbug.Event.MouseHoldHandler.clickPermissionTimerID);
							Misbug.Event.MouseHoldHandler.clickPermissionTimerID = window.setTimeout(function()
							{
								Misbug.Event.MouseHoldHandler.clickPermission = true;
							}, 800);
						}
						else
						{
							Misbug.Event.MouseHoldHandler.clickPermission = true;
						}
					});
					
					TabPanel.tabs[i].button.addEvent('mouseleave', function()
					{
						window.clearTimeout(Misbug.Event.MouseHoldHandler.timerID);
						
						if (!this.hasClass('active'))
						{
							this.fx.start({
								'color': Misbug.UI.colors['link']
							});
						}
					});
					
					TabPanel.tabs[i].button.fx.start({
						'color': Misbug.UI.colors['link']
					});
				}
			}
		}
	}
}

Misbug.UI.Scroller = function(arguments)
{
	if (arguments && arguments.container)
	{
		Misbug.UI.currentScroller = {
			container: arguments.container
		};
		
		Misbug.UI.currentScroller.container.addEvent('mouseenter', function(event)
		{
			Misbug.UI.currentScroller = {
				container:			this,
				content:	Misbug.DOM.GetFirstChildWithClassName(this, 'scroller-content', 'div'),
				page:				event.page
			};
			

			
			
			Misbug.UI.ScrollerDaemon();
		});
		
		Misbug.UI.currentScroller.container.addEvent('mouseleave', function()
		{
			Misbug.UI.currentScroller = false;
		});
		
		Misbug.UI.currentScroller.container.addEvent('mousemove', function(event)
		{
			if (Misbug.UI.currentScroller !== false)
			{
				Misbug.UI.currentScroller.page = event.page;
			}
		});
		
		Misbug.UI.currentScroller = false;
	}
}

Misbug.UI.CalculateStep = function(startDate, distance, startDistance)
{
	var maxTimeInterval = 1000;
	var x;
	
	var factor = ((x = (new Date()) - startDate) > maxTimeInterval ? maxTimeInterval : x) / maxTimeInterval;
	
	return((startDistance - distance) / startDistance * factor * 15)
}

Misbug.UI.ScrollerDaemon = function()
{
	if (Misbug.UI.currentScroller !== false)
	{
		Misbug.UI.currentScroller.position = Misbug.UI.currentScroller.container.getOffsets();
		Misbug.UI.currentScroller.size = Misbug.UI.currentScroller.container.getSize();
		
		Misbug.UI.currentScroller.scroll = {x: Misbug.UI.currentScroller.content.offsetLeft, y: Misbug.UI.currentScroller.content.offsetTop};
		Misbug.UI.currentScroller.contentSize = Misbug.UI.currentScroller.content.getSize();

		var x;
		
		Misbug.UI.currentScroller.distance = {
			top:	(x = Misbug.UI.currentScroller.page.y - Misbug.UI.currentScroller.position.y) < 0 ? 0 : x,
			bottom:	(x = Misbug.UI.currentScroller.size.y + Misbug.UI.currentScroller.position.y - Misbug.UI.currentScroller.page.y) < 0 ? 0 : x,
			left:	(x = Misbug.UI.currentScroller.page.x - Misbug.UI.currentScroller.position.x) < 0 ? 0 : x,
			right:	(x = Misbug.UI.currentScroller.size.x + Misbug.UI.currentScroller.position.x - Misbug.UI.currentScroller.page.x) < 0 ? 0 : x
		};
		
		Misbug.UI.currentScroller.startDistance = {
			x:	Math.round(Misbug.UI.currentScroller.size.x / 5),
			y:	Math.round(Misbug.UI.currentScroller.size.y / 2.9)
		};
		
		if ($type(Misbug.UI.ScrollerDaemon.startDateY) != 'date')
		{
			Misbug.UI.ScrollerDaemon.startDateY = new Date();
		}
		
//		document.title = Misbug.UI.currentScroller.scroll.y;
		
		if (Misbug.UI.currentScroller.size.y < Misbug.UI.currentScroller.contentSize.y)
		{
			if (Misbug.UI.currentScroller.distance.top < Misbug.UI.currentScroller.startDistance.y)
			{
				Misbug.UI.currentScroller.scroll.y +=  Misbug.UI.CalculateStep(Misbug.UI.ScrollerDaemon.startDateY, Misbug.UI.currentScroller.distance.top, Misbug.UI.currentScroller.startDistance.y);

				if (Misbug.UI.currentScroller.scroll.y > 0)
				{
					Misbug.UI.currentScroller.scroll.y = 0;
				}
			}
			else if (Misbug.UI.currentScroller.distance.bottom < Misbug.UI.currentScroller.startDistance.y)
			{
				Misbug.UI.currentScroller.scroll.y -= Misbug.UI.CalculateStep(Misbug.UI.ScrollerDaemon.startDateY, Misbug.UI.currentScroller.distance.bottom, Misbug.UI.currentScroller.startDistance.y);
				
				if (Misbug.UI.currentScroller.scroll.y < Misbug.UI.currentScroller.size.y - Misbug.UI.currentScroller.contentSize.y)
				{
					Misbug.UI.currentScroller.scroll.y = Misbug.UI.currentScroller.size.y - Misbug.UI.currentScroller.contentSize.y;
				}
			}
			else
			{
				Misbug.UI.ScrollerDaemon.startDateY = false;
			}
			
			Misbug.UI.currentScroller.content.setStyle('top', Math.round(Misbug.UI.currentScroller.scroll.y) + 'px');
		}

		
		window.clearTimeout(Misbug.UI.ScrollerDaemon.timerID);
		Misbug.UI.ScrollerDaemon.timerID = window.setTimeout(Misbug.UI.ScrollerDaemon, 10);
	}
	else
	{
		Misbug.UI.ScrollerDaemon.startDateX = false;
		Misbug.UI.ScrollerDaemon.startDateY = false;
	}
}
